//
// Created by LiuYou on 2021/10/31.
//

#include <iostream>
#include <unistd.h>
#include <pthread.h>


static pthread_t tid;


int main(int argc, char** argv) {


    pthread_create(
            &tid, nullptr,
            [](void* args) -> void* {

                int* test = new int( 1 );

                pthread_cleanup_push( [](void* args) {
                    printf( "执行线程栈清理。\n" );
                    int* ptr = static_cast<int*>(args);
                    delete ptr;
                }, test );

                    pthread_testcancel();


                    sleep( 10 );
                pthread_cleanup_pop( 1 );
                pthread_exit( nullptr );
            },
            nullptr );

    sleep( 5 );

    pthread_cancel( tid );
    pthread_join( tid, nullptr );


    return 0;
}
